package cn.cqautotest.cqviehelper.views.calendar;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.zhuangfei.timetable.listener.IWeekView;
import com.zhuangfei.timetable.listener.OnWeekItemClickedAdapter;
import com.zhuangfei.timetable.listener.OnWeekLeftClickedAdapter;
import com.zhuangfei.timetable.model.Schedule;
import com.zhuangfei.timetable.model.ScheduleEnable;
import com.zhuangfei.timetable.model.ScheduleSupport;
import com.zhuangfei.timetable.model.WeekViewEnable;
import com.zhuangfei.timetable.view.PerWeekView;
import com.zhuangfei.timetable.view.WeekView;

import java.util.ArrayList;
import java.util.List;

import cn.cqautotest.cqviehelper.R;


@SuppressLint("SetTextI18n")
public class MyWeekView extends WeekView implements WeekViewEnable<WeekView> {

    private static final String TAG = "WeekView";
    protected LayoutInflater mInflate;

    //周次的容器
    protected LinearLayout container;

    //根布局
    protected LinearLayout root;

    //左侧按钮
    protected LinearLayout leftLayout;

    //数据
    protected List<Schedule> dataSource;

    //布局保存
    protected List<LinearLayout> layouts;
    protected List<TextView> textViews;

    //当前周
    protected int curWeek = 1;
    protected int preIndex = 1;

    //多少项
    protected int itemCount = 20;

    protected IWeekView.OnWeekItemClickedListener onWeekItemClickedListener;
    protected IWeekView.OnWeekLeftClickedListener onWeekLeftClickedListener;

    public MyWeekView(Context context) {
        this(context, null);
    }

    public MyWeekView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mInflate = LayoutInflater.from(context);
        initView();
    }

    private void initView() {
        mInflate.inflate(R.layout.view_weekview, this);
        container = findViewById(R.id.id_weekview_container);
        root = findViewById(R.id.id_root);
        leftLayout = findViewById(R.id.id_weekview_leftlayout);
    }

    /**
     * 获取Item点击监听
     *
     * @return
     */
    public IWeekView.OnWeekItemClickedListener onWeekItemClickedListener() {
        if (onWeekItemClickedListener == null)
            onWeekItemClickedListener = new OnWeekItemClickedAdapter();
        return onWeekItemClickedListener;
    }

    /**
     * 设置Item点击监听
     *
     * @param onWeekItemClickedListener
     * @return
     */
    public MyWeekView callback(IWeekView.OnWeekItemClickedListener onWeekItemClickedListener) {
        this.onWeekItemClickedListener = onWeekItemClickedListener;
        return this;
    }

    /**
     * 获取左侧按钮点击监听
     *
     * @return
     */
    public IWeekView.OnWeekLeftClickedListener onWeekLeftClickedListener() {
        if (onWeekLeftClickedListener == null)
            onWeekLeftClickedListener = new OnWeekLeftClickedAdapter();
        return onWeekLeftClickedListener;
    }

    /**
     * 设置左侧按钮点击监听
     *
     * @param onWeekLeftClickedListener
     * @return
     */
    public MyWeekView callback(IWeekView.OnWeekLeftClickedListener onWeekLeftClickedListener) {
        this.onWeekLeftClickedListener = onWeekLeftClickedListener;
        return this;
    }

    /**
     * 设置当前周
     *
     * @param curWeek
     * @return
     */
    @Override
    public MyWeekView curWeek(int curWeek) {
        if (curWeek < 1) curWeek = 1;
        this.curWeek = curWeek;
        return this;
    }

    /**
     * 设置项数
     *
     * @param count
     * @return
     */
    @Override
    public MyWeekView itemCount(int count) {
        if (count <= 0) return this;
        this.itemCount = count;
        return this;
    }

    @Override
    public int itemCount() {
        return itemCount;
    }

    /**
     * 设置数据源
     *
     * @param list
     * @return
     */
    @Override
    public MyWeekView source(List<? extends ScheduleEnable> list) {
        data(ScheduleSupport.transform(list));
        return this;
    }

    /**
     * 设置数据源
     *
     * @param scheduleList
     * @return
     */
    @Override
    public MyWeekView data(List<Schedule> scheduleList) {
        if (scheduleList == null) return null;
        this.dataSource = scheduleList;
        return this;
    }

    /**
     * 获取数据源
     *
     * @return
     */
    @Override
    public List<Schedule> dataSource() {
        if (dataSource == null) dataSource = new ArrayList<>();
        return dataSource;
    }

    /**
     * 初次构建时调用，显示周次选择布局
     */
    @Override
    public MyWeekView showView() {
        if (curWeek < 1) curWeek(1);
        if (curWeek > itemCount()) curWeek = itemCount;
        container.removeAllViews();
        layouts = new ArrayList<>();
        textViews = new ArrayList<>();
        leftLayout.setOnClickListener(view -> onWeekLeftClickedListener().onWeekLeftClicked());
        for (int i = 1; i <= itemCount; i++) {
            final int tmp = i;
            View view = mInflate.inflate(R.layout.item_weekview, null);
            final LinearLayout perLayout = view.findViewById(R.id.id_perweekview_layout);
            TextView weekText = view.findViewById(R.id.id_weektext);
            TextView bottomText = view.findViewById(R.id.id_weektext_bottom);
            weekText.setText("第" + i + "周");
            if (i == curWeek) bottomText.setText("(本周)");
            PerWeekView perWeekView = view.findViewById(R.id.id_perweekview);
            perWeekView.setData(dataSource(), i);
            perLayout.setOnClickListener(view1 -> {
                mPerLayout = view1;
                resetBackground();
                preIndex = tmp;
                perLayout.setBackgroundResource(R.drawable.weekview_white);
                onWeekItemClickedListener().onWeekClicked(tmp);
            });
            layouts.add(perLayout);
            textViews.add(bottomText);
            container.addView(view);
        }
        if (curWeek > 0 && curWeek <= layouts.size()) {
            layouts.get(curWeek - 1).setBackgroundResource(R.drawable.weekview_thisweek);
        }
        return this;
    }

    /**
     * 当前周被改变后可以调用该方式修正一下底部的文本
     *
     * @return
     */
    @Override
    public MyWeekView updateView() {
        if (layouts == null || layouts.size() == 0) return this;
        if (textViews == null || textViews.size() == 0) return this;

        for (int i = 0; i < layouts.size(); i++) {
            if (curWeek - 1 == i) {
                textViews.get(i).setText("(本周)");
            } else {
                textViews.get(i).setText("");
            }
            layouts.get(i).setBackgroundColor(0x00000000);
        }
        if (curWeek > 0 && curWeek <= layouts.size()) {
            layouts.get(curWeek - 1).setBackgroundResource(R.drawable.weekview_thisweek);
        }
        return this;
    }

    /**
     * 重置背景色
     */
    public void resetBackground() {
        // 如果不进行如下判断将有可能导致下标越界
        if (curWeek > 0 && curWeek <= layouts.size()) {
            layouts.get(preIndex - 1).setBackgroundColor(0x00000000);
            layouts.get(curWeek - 1).setBackgroundResource(R.drawable.weekview_thisweek);
        }
    }

    private View mPerLayout;

    /**
     * 滚动到预览周
     */
    public void scrollToPerLayout() {
        if (mPerLayout != null) {
            mPerLayout.setFocusable(true);
            mPerLayout.setFocusableInTouchMode(true);
            mPerLayout.requestFocus();
        }
    }

    /**
     * 滚动到当前周
     */
    public void scrollToCurWeek() {
        // 如果不进行如下判断将有可能导致下标越界
        if (curWeek > 0 && curWeek <= layouts.size()) {
            layouts.get(curWeek - 1).setFocusable(true);
            layouts.get(curWeek - 1).setFocusableInTouchMode(true);
            layouts.get(curWeek - 1).requestFocus();
        }
    }

    /**
     * 隐藏左侧设置当前周的控件
     */
    public MyWeekView hideLeftLayout() {
        leftLayout.setVisibility(View.GONE);
        return this;
    }

    /**
     * 设置控件的可见性
     *
     * @param isShow true:显示，false:隐藏
     */
    @Override
    public MyWeekView isShow(boolean isShow) {
        if (isShow) {
            setVisibility(VISIBLE);
        } else {
            setVisibility(GONE);
        }
        return this;
    }

    /**
     * 判断该控件是否显示
     *
     * @return
     */
    @Override
    public boolean isShowing() {
        if (getVisibility() == GONE) return false;
        return true;
    }
}
